import socket


def main():
    # 创建套接字
    tcp_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    # 绑定本地信息
    tcp_server.bind(("", 502))
    # 让默认的套接字由主动变被动
    tcp_server.listen(128)
    while True:
        print("等待一个新的客户端的到来...")
        # 等待client连接
        new_client_socket, client_addr = tcp_server.accept()
        print("一个新的客户端已经到来：%s " % str(client_addr))
        while True:
            # 接首客户端发送过来的请求
            recv_data = new_client_socket.recv(1024)
            print("客户端发送过来的请求是：%s" % recv_data.decode("gbk"))
            if recv_data:
                new_client_socket.send("服务器已收到".encode("gbk"))
                # print("收到")
            else:
                break
        new_client_socket.close()
    tcp_server.close()


if __name__ == "__main__":
    main()
